// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userPhone: '',
    password: '',
  },

  //登录
  login: function (e) {
    var that = this
    console.log("login- userPhone: " + this.data.userPhone + " password: " + this.data.password)
    var domain = getApp().globalData.myHttp
    var url = domain + '/user/checkLogin'
    var opt = {
      phone: this.data.userPhone,
      password: this.data.password,
      imei: '',
      isDoctor: false
    }
    var phoneReg = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/;
    if (!phoneReg.test(this.data.userPhone)) {
      wx.showToast({
        title: '输入正确手机号',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    console.log(this.data.password)
    if (this.data.password.length < 6) {
      wx.showToast({
        title: '请输入密码!',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    wx.request({
      url: url,
      method: "GET",
      header: {
        'content-type': 'application/json',
      },
      data: opt,
      success: function (res) {
        console.log(res.data)
        var data = res.data
        if (data.success) {
          // 缓存用户数据
          wx.setStorageSync('user', data.obj.user)
          //token
          wx.setStorageSync('token',data.obj.token)
          // 保存用户账号
          getApp().globalData.userPhone = data.obj.user.userPhone;
          console.log("登录成功")
          wx.navigateBack()
        }
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 2000
        })
      }

    })
  },

  // 获取输入账号
  // 给账号的input加上bindinput="usernameInput"，赋值
  userPhoneInput: function (e) {
    this.setData({
      userPhone: e.detail.value
    })
  },

  // 获取输入密码
  // 给密码的input加上bindinput="passwordInput"，赋值
  passwordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },

  // 注册跳转页面
  bindzhuce: function () {
    wx.navigateTo({
      url: '../zhuce/zhuce',
    })
  },
  // 找回密码
  bindmima: function () {
    wx.navigateTo({
      url: '../findmima/findmima',
    })
  },

  /**
   * 验证登入状态
   */
  onLoad: function (e) {
    var user = wx.getStorageSync("user")

    console.log(user)
    // if (user !== '' && user !== null & user !== undefined) {
    //   //已经登录
    //   console.log("已经登录")
    //   wx.switchTab({
    //     url: '/pages/register/register',
    //   })
    // }
  },



  //微信登录
  wxLogin() {
    wx.navigateTo({
      url: '../wxlogin/wxlogin',
    })
    console.log("用户微信登录")
  },




})